package org.e_gold.service.impl;

import org.apache.commons.lang.StringUtils;
import org.e_gold.dao.ConfigDAO;
import org.e_gold.model.Config;
import org.e_gold.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Thang Nguyen
 */
@Service
public class ConfigServiceImpl implements ConfigService {

	@Autowired
	private ConfigDAO configDAO;

	public static enum betConfig{
		BET_TIMES,
		BET_AMOUNT_PER_TIME,
		BET_CURRENT_COUNT,
		BEST_STRING_SIMILARITY,
		BET_RATE_MIN,
		BET_RATE_MAX,
		SAFE_MODE,
		CURRENT_GOAL,
		TOTAL_GOAL,
		BET_SETTING,
		BASE_AMOUNT,
		WON_PERCENT;
	}

	@Override
	public Config save(Config config) {
		Config cfg = null;
		if(config.getId() != null){
			cfg = configDAO.update(config);
		}else{
			cfg = configDAO.insert(config);
		}

		return cfg;
	}

	@Override
	public Config get(String name) {
		return get(name, true);
	}

	public Config get(String name, boolean createNew){
		Config cfg = configDAO.getUniqueByPropertyName("name", name);
		if(cfg == null && createNew){
			cfg = Config.builder(name, "");
		}
		return cfg;
	}

	@Override
	public void set_bet_times(Object obj){
		Integer times = Integer.parseInt(obj.toString());
		Config cfgBetTimes = get(betConfig.BET_TIMES.toString());
		cfgBetTimes.setValue(times.toString());
		save(cfgBetTimes);
	}

	@Override
	public Integer get_bet_times(){
		Config cfgBetTimes = get(betConfig.BET_TIMES.toString());
		Integer times = Integer.parseInt(cfgBetTimes.getValue());

		return times;
	}

	@Override
	public void set_bet_amount_per_time(Object obj){
		Float amount = Float.parseFloat(obj.toString());
		Config cfgAmount = get(betConfig.BET_AMOUNT_PER_TIME.toString());
		cfgAmount.setValue(amount.toString());
		save(cfgAmount);
	}

	@Override
	public Float get_bet_amount_per_time(){
		Config cfgAmount = get(betConfig.BET_AMOUNT_PER_TIME.toString());
		if(StringUtils.isEmpty(cfgAmount.getValue())){
			cfgAmount.setValue("0f");
		}
		Float amount = Float.parseFloat(cfgAmount.getValue());

		return amount;
	}

	@Override
	public void set_bet_current_count(Object obj) {
		Integer count = Integer.parseInt(obj.toString());
		Config cfgCount = get(betConfig.BET_CURRENT_COUNT.toString());
		cfgCount.setValue(count.toString());
		save(cfgCount);
	}

	@Override
	public Integer get_bet_current_count() {
		Config cfgCount = get(betConfig.BET_CURRENT_COUNT.toString());
		Integer count = Integer.parseInt((cfgCount.getValue()));

		return count;
	}

	@Override
	public Double get_string_similarity() {
		Config cfgRate = get(betConfig.BEST_STRING_SIMILARITY.toString());
		Double rate = Double.parseDouble(cfgRate.getValue());

		return rate;
	}

	@Override
	public void set_string_similarity(Object obj) {
		Double rate = Double.parseDouble(obj.toString());
		Config cfgRate = get(betConfig.BEST_STRING_SIMILARITY.toString());
		cfgRate.setValue(rate.toString());
		save(cfgRate);
	}

	@Override
	public String get_bet_rule() {
		Config cfgRule = get(betConfig.BET_SETTING.toString());

		return cfgRule.getValue();
	}
}
